﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IndexDefinitionRepositoryTests.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the IndexDefinitionRepositoryTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EFRepositoryTests
{
  using System;
  using System.Configuration;
  using System.Data.Entity;
  using System.Data.Entity.Validation;
  using System.Linq;
  using System.Transactions;
  using BuildMeA;
  using DocProcessingRepository.Repositories;
  using Entities;
  using FluentAssertions;
  using NUnit.Framework;

  [Category("Integration")]
  [TestFixture]
  public class IndexDefinitionRepositoryTests
  {
    private const Int32 DefaultMailSortSettingId = 7;

    private TransactionScope transactionScope;
    private IndexDefinitionRepository indexDefinitionRepository;
    private ApplicationUserRepository applicationUserRepository;
    private MediaDefinitionRepository mediaDefinitionRepository;

    private MediaDefinition mediaDefinition;
    private IndexDefinition indexDefinition;

    [SetUp]
    public void Setup()
    {
      this.transactionScope = new TransactionScope();

      String connectionString = ConfigurationManager.ConnectionStrings["BBOS"].ConnectionString;
      this.indexDefinitionRepository = new IndexDefinitionRepository(connectionString);
      this.applicationUserRepository = new ApplicationUserRepository(connectionString);
      this.mediaDefinitionRepository = new MediaDefinitionRepository(connectionString);

      ApplicationUser user = BuildMeA.ApplicationUser("user1");
      this.applicationUserRepository.Create(user);

      this.indexDefinition = BuildMeA.IndexDefinition(
        1,
        "IndexDefinitionName",
        "ExStreamSymbolicIndexName",
        "ExStreamSymbolicDataName",
        DefaultMailSortSettingId);

      this.mediaDefinition =
        BuildMeA.MediaDefinition(1, 1, 1, 1, user.Id, "test", false, false).WithIndexDefinition(this.indexDefinition);
    }

    [TearDown]
    public void TearDown()
    {
      this.transactionScope.Dispose();
    }

    [Test]
    public void GivenAMediaDefinitionId_Data_WhenISetTheMailSortSetting_ItIsUpdatedInTheDatabase()
    {
      this.mediaDefinitionRepository.Create(this.mediaDefinition);

      IndexDefinition existingIndexDefinition =
        this.indexDefinitionRepository.Entities.Single(
          id => id.IndexDefinitionID == this.indexDefinition.IndexDefinitionID);

      existingIndexDefinition.MailSortSettingID.Should().Be(DefaultMailSortSettingId);

      this.indexDefinitionRepository.UpdateMailSortSetting(this.mediaDefinition.MediaDefID, 8);

      existingIndexDefinition =
        this.indexDefinitionRepository.Entities
        .AsNoTracking()
        .Single(id => id.IndexDefinitionID == this.indexDefinition.IndexDefinitionID);

      existingIndexDefinition.MailSortSettingID.Should().Be(8);
    }

    [Test]
    public void GivenAMediaDefinitionId_Data_WhenISetTheMailSortSettingToNull_ItIsUpdatedInTheDatabase()
    {
      this.mediaDefinitionRepository.Create(this.mediaDefinition);

      IndexDefinition existingIndexDefinition =
        this.indexDefinitionRepository.Entities.Single(
          id => id.IndexDefinitionID == this.indexDefinition.IndexDefinitionID);

      existingIndexDefinition.MailSortSettingID.Should().Be(DefaultMailSortSettingId);

      this.indexDefinitionRepository.UpdateMailSortSetting(this.mediaDefinition.MediaDefID, null);

      existingIndexDefinition =
        this.indexDefinitionRepository.Entities
        .AsNoTracking()
        .Single(id => id.IndexDefinitionID == this.indexDefinition.IndexDefinitionID);

      existingIndexDefinition.MailSortSettingID.Should().Be(null);
    }

    [Test]
    public void GivenAMediaDefinitionId_Data_WhenISetTheMailSortSettingToZero_AnArgumentExceptionIsRaised()
    {
      Action act = () => this.indexDefinitionRepository.UpdateMailSortSetting(this.mediaDefinition.MediaDefID, 0);

      act.ShouldThrow<ArgumentException>();
    }

    [Test]
    public void GivenAMediaDefinitionId_Data_WhenISetTheMailSortSettingToANegativeNumber_AnArgumentExceptionIsRaised()
    {
      Action act = () => this.indexDefinitionRepository.UpdateMailSortSetting(this.mediaDefinition.MediaDefID, -1);

      act.ShouldThrow<ArgumentException>();
    }
  }
}